package com.chanyachting.emall.action.periodical;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.chanyachting.emall.dao.PeriodicalDAO;
import com.chanyachting.emall.domain.Member;
import com.chanyachting.emall.domain.Newsactivity;
import com.chanyachting.emall.domain.Periodical;
import com.chanyachting.emall.form.PeriodicalForm;
import com.chanyachting.emall.service.MemberService;
import com.chanyachting.emall.util.RequestUtil;

public class PeriodicalAction extends DispatchAction {

	
	
	
	public ActionForward savePNews(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		String newsArray=ru.getParameter("menuArray", "");
		
		int pid=Integer.parseInt(ru.getParameter("pid", "0")) ;
		
		if(!newsArray.trim().equals("")){
			
			String newsIdArray []=newsArray.split(",");
			
			for(int i=0;i<newsIdArray.length;i++){
				
				int newsId=Integer.parseInt(newsIdArray[i]);
				
				Newsactivity na=new Newsactivity(newsId);
				
				if(na!=null){
					
					na.setPeriodical(pid);  //设置新闻的期刊编号
					
					na.update(); //修改新闻对象
				}

			}
			
		}

		return mapping.findForward("");
	}
	
	
	
	public ActionForward newsActivityList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	
		RequestUtil ru=new RequestUtil(request);
		
		//从session 中获得会员对象
		Member member=(Member)ru.getSessionObject(MemberService.MEMBER_IN_SESSION, new Member());
		
		int memberId=member.getId();;
		
		ru.setAttribute("newsactivityList", Newsactivity.listByMemberIdAndStatus(memberId, "02"));
		
		return mapping.findForward("newsActivityList");
	}
	
	
	
	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		

		if(request.getMethod().equals("POST")){
			
			PeriodicalForm pform=(PeriodicalForm)form;
			
			if(pform.validate(mapping, request).isEmpty()){
				
				Periodical pe=new Periodical();
				
				BeanUtils.copyProperties(pe, pform);
				
				new PeriodicalDAO().save(pe);
				
				new RequestUtil(request).setSuccessMsg();
				
			}
			
		}
		
		return mapping.findForward("create");
	}
	
	
	
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		Periodical pe=new Periodical(id);
		
		if(request.getMethod().equals("POST")){
			
			PeriodicalForm pform=(PeriodicalForm)form;
			
			if(pform.validate(mapping, request).isEmpty()){
				
				Periodical pecal=new Periodical();
				
				BeanUtils.copyProperties(pecal, pform);
				
				new PeriodicalDAO().update(pecal);
				
				new RequestUtil(request).setSuccessMsg();
				
			}
			
		}
		
		//重新加载期刊对象
		pe=(Periodical) new PeriodicalDAO().load(Periodical.class, pe.getId());
		
		ru.setAttribute("periodical", pe);
		
		return mapping.findForward("update");
	}
	
	
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		Periodical pe=new Periodical(id);
		
		pe.setMagazineId("");
		
		new PeriodicalDAO().delete(pe);
		
		return mapping.findForward("delete");
	}
	
	
	
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		List list=new PeriodicalDAO().find("from Periodical");
		
		ru.setAttribute("periodicalList", list);
		
		return mapping.findForward("list");
	}
	
	

}
